package lib.XML;

import java.io.FileReader;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

import java.util.ArrayList;

import lib.Base.Friend;
import lib.Base.Message;
import lib.Base.Profile;
import lib.Base.PublicGroups;
import lib.Base.Settings;

public class XMLParser {
	/**
	 * Class to parse xml document's
	 */
	public XMLParser() {

	}

	public ArrayList<Friend> ParseFriendlist(String xmlFile) {
		try {
			XMLReader xmlReader = XMLReaderFactory.createXMLReader();
			FileReader reader = new FileReader(xmlFile);
			InputSource inputSource = new InputSource(reader);
			FriendlistContentHandler handler = new FriendlistContentHandler();
			xmlReader.setContentHandler(handler);
			xmlReader.parse(inputSource);
			return handler.getFriendlist();
		} catch (Exception e) {
			return null;
		}
	}

	public ArrayList<Message> ParseConversation(String xmlFile) {
		try {
			XMLReader xmlReader = XMLReaderFactory.createXMLReader();
			FileReader reader = new FileReader(xmlFile);
			InputSource inputSource = new InputSource(reader);
			ConversationContentHandler handler = new ConversationContentHandler();
			xmlReader.setContentHandler(handler);
			xmlReader.parse(inputSource);
			return handler.getConversation();
		} catch (Exception e) {
			return null;
		}
	}

	public PublicGroups ParsePublicGroups(String xmlFile) {
		try {
			XMLReader xmlReader = XMLReaderFactory.createXMLReader();
			FileReader reader = new FileReader(xmlFile);
			InputSource inputSource = new InputSource(reader);
			PublicGroupsContentHandler handler = new PublicGroupsContentHandler();
			xmlReader.setContentHandler(handler);
			xmlReader.parse(inputSource);
			return handler.getPublicGroups();
		} catch (Exception e) {
			return null;
		}
	}

	public Profile ParseProfile(String xmlFile) {
		try {
			XMLReader xmlReader = XMLReaderFactory.createXMLReader();
			FileReader reader = new FileReader(xmlFile);
			InputSource inputSource = new InputSource(reader);
			ProfileContentHandler handler = new ProfileContentHandler();
			xmlReader.setContentHandler(handler);
			xmlReader.parse(inputSource);
			return handler.getProfile();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public Settings ParseSettings(String xmlFile) {
		try {
			XMLReader xmlReader = XMLReaderFactory.createXMLReader();
			FileReader reader = new FileReader(xmlFile);
			InputSource inputSource = new InputSource(reader);
			SettingsContentHandler handler = new SettingsContentHandler();
			xmlReader.setContentHandler(handler);
			xmlReader.parse(inputSource);
			return handler.getSettings();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}